import http from '../../apis/api.js';
import util from '../../utils/util.js';
let self;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    /**分页数据 */
    jzUser:false,
    jyUser:false,
    pagination: {
      currentPage: 1,
      pageSize: 6,
      more: 0
    }, 
    keywords: '',
    loading: false,
    materialList:[],
    isSign:null,
    model:{
      hidden:true,
      id:'',
      autNums:1
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self=this;
    self.data.isSign = Number(options.isSign),
    self.setData({
      isSign: self.data.isSign
    })
    util.getRole().then(res=>{
      self.data.jzUser = res.indexOf('jz_user') > -1;
      self.data.jyUser = res.indexOf('jy_user')>-1;
      self.setData({
        jzUser: self.data.jzUser,
        jyUser: self.data.jyUser
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    self.data.keywords = "";
    self.data.pagination.currentPage = 1;
    self.data.materialList = [];
    self.getMaterialList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (self.data.pagination.more > 0) {
      self.data.pagination.currentPage++;
      self.getMaterialList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**获取零件列表*/ 
  getMaterialList() {
    let postData = {
      currentPage: self.data.pagination.currentPage,
      pageSize: self.data.pagination.pageSize,
      isSign: self.data.isSign
    }
    self.setData({
      'loading': true
    })
    http.materialList(postData).then(res => {
      self.data.materialList = self.data.materialList.concat(res.content.records);
      self.data.pagination.more = Number(res.content.current) * Number(res.content.size) < res.content.total ? 1 : 0;
      self.setData({
        'loading': false,
        'materialList': self.data.materialList,
        'pagination': self.data.pagination
      })
    })
  },
  changeState(e){
    self.data.isSign = e.currentTarget.dataset.state ? Number(e.currentTarget.dataset.state) : null;
    self.data.pagination.currentPage = 1;
    self.data.materialList = [];
    self.setData({
      isSign: self.data.isSign
    })
    self.getMaterialList();
  },
  goRouter(e){
    console.log(e);
    let type = e.currentTarget.dataset.type;
    let url
    switch (type){
      case 'add':
        url = `/pages/addMaterial/addMaterial?type=add`
      break;
    }
    wx.navigateTo({
      url,
    })
  },
  optionHandle(e){
    console.log(e);
    let dataset = e.currentTarget.dataset;
    switch (dataset.type){
      case 'sign':
        self.data.model.hidden = false;
        self.data.model.id = dataset.item.id;
        self.data.model.autNums = dataset.item.nums ? dataset.item.nums:1;
        self.setData({
          model: self.data.model
        })
      break;
      case 'detail':
      wx.navigateTo({
        url: `/pages/materialDetail/materialDetail?id=${dataset.id}`,
      })
        break;
    }
  },
  /**modal点击取消*/
  cancel() {
    self.data.model.hidden = true;
    self.setData({
      model: self.data.model
    })
  },
  /**获取input的值*/ 
  getValus(e){
    console.log(e)
    self.data.model.autNums = e.detail.value;
    self.setData({
      model: self.data.model
    })
  },
  /**modal点击确认 */
  confirm(e){
    if (self.data.model.autNums===''){
      wx.showToast({
        title: '请输入实际签收数量',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (self.data.model.autNums<=0) {
      wx.showToast({
        title: '签收数量不能小于或等于0',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    let postData={
      id:self.data.model.id,
      autNums: self.data.model.autNums,
      isSign: 1
    }
    http.updateMaterial(postData).then(res => {
      self.data.model.hidden = true;
      wx.showToast({
        title: '签收成功',
        icon: 'none',
        duration: 2000
      })
      self.setData({
        model:self.data.model
      })
      self.data.pagination.currentPage = 1;
      self.data.materialList= [];
      self.getMaterialList();
    })
  }
})